package meconsea.life.admin.accessor;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import meconsea.life.admin.entity.Indicate;
import meconsea.life.sys.accessor.BaseAccessor;
import meconsea.life.sys.accessor.IAccessor;

import com.google.inject.name.Named;
import com.wideplay.warp.persist.TransactionType;
import com.wideplay.warp.persist.Transactional;
import com.wideplay.warp.persist.dao.Finder;
import com.wideplay.warp.persist.dao.FirstResult;
import com.wideplay.warp.persist.dao.MaxResults;

public class IndicateAccessor extends BaseAccessor<Indicate, String> implements
		IAccessor<Indicate, String> {
	public IndicateAccessor() {
		super(Indicate.class);
	}

	@Finder(namedQuery = Indicate.FIND_IN_CLSCODE)
	public Indicate getIndicateByClsCode(@Named("clsCode") String clsCode) {
		return null;
	}
	
	@Finder(namedQuery = Indicate.FIND_IN_CLSCODES)
	public List<Indicate> getIndicateByClsCodes(@Named("clsCodeList") List<String> clsCodeList){
		return null;
	}

	@Finder(namedQuery = Indicate.FIND_IN_PARENT)
	public List<Indicate> getIndicateByParentCls(
			@Named("parentCls") String parentCls) {
		return null;
	}

	@Finder(namedQuery = Indicate.FIND_IN_CHILD)
	public List<Indicate> getChildIndicateByOrderLevel(
			@Named("clsLevel") int clsLevel, @Named("clsOrder") int clsOrder,@FirstResult int first,@MaxResults int max) {
		return null;
	}
   
	@Finder(namedQuery = Indicate.FIND_IN_SAME_LEVEL)
	public List<Indicate> getLatterBro(@Named("parentCls") String  parentCls,@Named("clsOrder") int clsOrder){
		return null;
	}

	@Finder(namedQuery = Indicate.FIND_IN_MAXORDER)
	public Indicate getIndicateMaxOrder(@FirstResult int first,
			@MaxResults int max) {
		return null;
	}

	@Transactional
	public Indicate addIndicate(Indicate indicate) {
		// 获取其parentClsCod
		Indicate parentInd = getIndicateByClsCode(indicate.getParentCls());
		//获取指标的Path，截至到Root自动跳出
		String clsPath = parentInd.getClsPath()+parentInd.getClsCode()+"|";
		
		/**
		 * 1.首先获取父节点的order。
		 * 2.获取父节点的直接孩子节点。
		 * 3.孩子节点最大的order+1.
		 * 4.update order+1 where order >= 3.order
		 */
		
		int newOrder = parentInd.getClsOrder()+1;
		
		List<Indicate> childInd = getIndicateByParentCls(parentInd.getClsCode());
		
		if(childInd!= null && childInd.size()> 0){
			Indicate maxChildInd = childInd.get(childInd.size()-1);
			newOrder = maxChildInd.getClsOrder()+1;
		}
		String updateOrder = " UPDATE Indicate SET clsOrder = clsOrder+1 WHERE clsOrder >= "
				+ newOrder;
		Query query = emp.get().createQuery(updateOrder);
		query.executeUpdate();
		indicate.setClsLevel(parentInd.getClsLevel() + 1);
		/**
		// 获取父节点的兄弟节点
		List<Indicate> sameLevel = getIndicateSameLevel(
				parentInd.getClsLevel(), parentInd.getClsOrder(), 0, 1);
		if (sameLevel == null || sameLevel.size() == 0) {// 没有兄弟节点
			Indicate maxOrderInd = getIndicateMaxOrder(0, 1);
			indicate.setClsOrder(maxOrderInd.getClsOrder() + 1);
		} else {// 更新兄弟节点之后的所有Order,
			Indicate sameOrder = sameLevel.get(0);
			int maxO = sameOrder.getClsOrder();
			System.out.println(maxO+"------------");
			String updateOrder = " UPDATE Indicate SET clsOrder = clsOrder+1 WHERE clsOrder >= "
					+ maxO;
			Query query = emp.get().createQuery(updateOrder);
			query.executeUpdate();
			indicate.setClsOrder(maxO);
		}
		**/
		indicate.setClsOrder(newOrder);
		indicate.setClsPath(clsPath);
		return emp.get().merge(indicate);
	}

	@Transactional
	public void removeIndicate(Indicate indicate) {
		EntityManager em = emp.get();
		Indicate rInd = this.find(indicate.getId());
		/**
		 * 判断有无兄弟节点
		 * 1. 有兄弟节点，取出最近的兄弟节点的latterBro.order。(parent=rInd.parent and order > rInd.order)
		 *     1） 删除大等于rInd.clsOrder并且小于latterBro.clsOrder。
		 *     2)  update order- (latterBro.clsOrder - rInd.clsOrder) where clsOrder >= latterBro.clsOrder;
		 * 2.无兄弟节点，找出最近的上级节点。(latterParentBro.clsOrder) clsLevel < rInd.clsLevel and clsOrder > rInd.clsOrder limit 0,1
		 *    1)  delete >= rInd.clsOrder < latterParentBro.clsOrder;
		 *    2) update order -(latterParentBro.clsOrder - rInd.clsOrder) where clsOrder >= latterParentBro.clsOrder;
		 * 3. 无兄弟节点 无最近的上级节点：
		 *    delete >= rInd.clsOrder
		 */
		List<Indicate> latterBro = getLatterBro(rInd.getParentCls(), rInd.getClsOrder());
		int mimOrder = 0;
		int lastOrder = 0;
		String delSql = "";
		String updateSql = "";
		if(latterBro != null && latterBro.size() > 0){
			Indicate _t = latterBro.get(0);
			lastOrder = _t.getClsOrder();
			mimOrder = lastOrder-rInd.getClsOrder();
			delSql = "DELETE FROM Indicate where clsOrder >="+rInd.getClsOrder()+" and clsOrder < "+lastOrder;
			updateSql = "UPDATE Indicate set clsOrder =clsOrder-" + mimOrder +" where clsOrder >="+lastOrder;
			Query query = emp.get().createQuery(delSql);
			query.executeUpdate();
			Query u_query = emp.get().createQuery(updateSql);
			u_query.executeUpdate();
		}else{
			List<Indicate> latterParentBroL = getChildIndicateByOrderLevel(rInd.getClsLevel(),rInd.getClsOrder(),0,1);
			if(latterParentBroL != null && latterParentBroL.size() > 0){
				Indicate _t = latterParentBroL.get(0);
				lastOrder = _t.getClsOrder();
				mimOrder = lastOrder-rInd.getClsOrder();
				delSql = "DELETE FROM Indicate where clsOrder >="+rInd.getClsOrder()+" and clsOrder < "+lastOrder;
				updateSql = "UPDATE Indicate set clsOrder =clsOrder-" + mimOrder +" where clsOrder >="+lastOrder;
				Query query = emp.get().createQuery(delSql);
				query.executeUpdate();
				Query u_query = emp.get().createQuery(updateSql);
				u_query.executeUpdate();
			}else{
				delSql = "DELETE FROM Indicate where clsOrder >="+rInd.getClsOrder();
				Query query = emp.get().createQuery(delSql);
				query.executeUpdate();
			}
		} 
		
	}
}
